﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OS
{
    static class Drum
    {
        private static Track[] drumMemory = new Track[100];

        /// <summary>
        /// Initialization method to load the memory array with Tracks
        /// </summary>
        public static void Initialize()
        {
            
            for (int i = 0; i < drumMemory.Length; i++)
            {
                drumMemory[i] = new Track(i);
            }
        }

        /// <summary>
        /// Getter/setter for the drumMemory Track array
        /// </summary>
        public static Track[] DrumMemory
        {
            get { return drumMemory; }
            set { drumMemory = value; }
        }

        /// <summary>
        /// Writes to a given track
        /// </summary>
        public static void WriteToTrack(Track t, String data)
        {
            drumMemory[t.TrackID].Data = data.ToCharArray();
            drumMemory[t.TrackID].TrackID = t.TrackID;
            drumMemory[t.TrackID].IsFree = false;
        }

        /// <summary>
        /// Clears a given track
        /// </summary>
        /// <param name="trackID">The ID of the track to clear</param>
        public static void ClearTrack(int trackID)
        {
            drumMemory[trackID] = new Track(trackID);
            drumMemory[trackID].IsFree = true;
        }

        /// <summary>
        /// Checks to see if a given track is free
        /// </summary>
        /// <param name="trackID">The ID of the track to check</param>
        /// <returns>A boolean value for whether the specified track is free</returns>
        public static bool IsTrackFree(int trackID)
        {
            return drumMemory[trackID].IsFree;
        }

        /// <summary>
        /// Returns the first free track on the drum
        /// </summary>
        /// <returns>A track object</returns>
        public static Track GetFreeTrack()
        {
            for (int i = 0; i < drumMemory.Length; i++)
            {
                if (IsTrackFree(i))
                {
                    return drumMemory[i];
                }
            }

            Console.WriteLine("No free drum tracks.");
            return null;
        }

        /// <summary>
        /// Returns the track with the specified ID.
        /// </summary>
        /// <param name="id">The ID of the track to retrieve</param>
        /// <returns>The track with the specified ID.</returns>
        public static Track GetTrack(int id)
        {
            return drumMemory[id];
        }
    }
}
